import {
    Dispatch
} from 'redux';

import Ajax from './../../http';

import * as types from './../types/iap';

interface ResultPayload {
    productId: string,
    transactionId: string,
    transactionReceipt: string
}

export default {
    //获取支付项
    [types.getGoldPlans]() {
        return async (dispatch: Dispatch) => {
            const _para = {
                planType:`subscribe`,
                platform: `android`
            }
            try {
                const res = (
                    await Ajax.get(
                        `member/pay/type/plan`, _para
                    )
                )
                dispatch({
                    data: res,
                    type: types.getGoldPlans
                })
            }
            catch (e) {
                return Promise.reject(e)
            }
        }
    },
    //获取消耗类型
    [types.getConsumablePlans]() {
        return async (dispatch: Dispatch) => {
            const _para = {
                planType:`consumable`,
                platform: `android`
            }
            try {
                const res = await  Ajax.get(
                    `member/pay/type/plan`,_para
                )

                dispatch({
                    data: res,
                    type: types.getConsumablePlans
                })
            }
            catch (e) {
                return Promise.reject(e)
            }
        }
    },
    //保存支付结果
    [types.savePayResult](payload: ResultPayload) {
        return async () => {
            return Ajax.post(
                `goldmember/payment/android`, payload
            )
        }
    },
    //检测支付状态
    [types.checkPayResult](transactionId:string) {
        return async () => {
            return Ajax.get(`transaction/paid`,{
                transactionId:transactionId
            })
        }
    }
}